package ruifight.member.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import ruifight.member.model.ShopAccount;
import ruifight.member.model.ShopAccountExample;
import ruifight.member.model.ShopInfo;
import ruifight.member.model.ShopInfoExample;
import ruifight.member.model.TransactionRecord;
import ruifight.member.service.ShopAccountService;
import ruifight.member.service.ShopInfoService;
import ruifight.member.service.TransactionRecordService;


@Controller
@RequestMapping("/interface")
public class TranscationRecordController {

	private static final Logger logger = LoggerFactory.getLogger(TranscationRecordController.class);

	@Autowired
	public TransactionRecordService transactionRecordService;
	@Resource
    private ShopAccountService shopAccountService;
    @Resource
    private ShopInfoService shopInfoService;
	/**
	 * 获取充值记录列表
	 * @return
	 */
	@RequestMapping("/getTransactionRecordList")
	@ResponseBody
	public Map<String, Object> getTransactionRecordList(@RequestBody Map<String, Object> paramMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		String cur_page= paramMap.get("cur_page").toString();
		String page_size= paramMap.get("page_size").toString();
		PageInfo<Map<String, Object>> pageInfo = transactionRecordService.getTransactionRecordList(Integer.parseInt(cur_page), Integer.parseInt(page_size), paramMap);
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return map;
	}
	/**
	 * 确认交易
	 * @return
	 */
	@RequestMapping("/submitTransaction")
	@ResponseBody
    public Map<String, Object> submitTransaction(@RequestBody Map<String, Object> paramMap){
    	Map<String, Object> map = new HashMap<String, Object>();
    	try{
    		Long transactionRecordId=Long.parseLong(paramMap.get("transactionRecordId").toString());
        	TransactionRecord transactionRecord=transactionRecordService.selectByPrimaryKey(transactionRecordId);
        	ShopAccountExample shopAccountExample=new ShopAccountExample();
        	shopAccountExample.createCriteria()
        		.andShopInfoIdEqualTo(transactionRecord.getShopInfoId());
        	ShopAccount shopAccount=shopAccountService.selectOneByExample(shopAccountExample);
        	ShopInfoExample shopInfoExample=new ShopInfoExample();
        	shopInfoExample.createCriteria()
        		.andShopInfoIdEqualTo(transactionRecord.getShopInfoId());
        	ShopInfo shopInfo=shopInfoService.selectOneByExample(shopInfoExample);
        	transactionRecordService.submitTransaction(transactionRecord,shopAccount,shopInfo);
			map.put("state",1);
			map.put("msg","操作成功");
		}catch (Exception e){
			map.put("state",0);
			map.put("msg","操作失败");
			e.printStackTrace();
    	}
		return map;
    }
}
